<template>
    <div class="game-display">
      <div class="carousel" ref="carousel">
        <div v-for="(image, index) in displayedImages" :key="index" :class="{ 'active': currentIndex === index }">
          <img :src="image" alt="Game Image">
        </div>
      </div>
      
      <button @click="prevImage" :disabled="currentIndex === 0">上一张</button>
      <button @click="nextImage" :disabled="currentIndex === images.length - 1">下一张</button>
  
      <div class="game-description">
        <h2>{{ gameTitle }}</h2>
        <p>{{ gameDescription }}</p>
        
        <div class="download-button">
          <button @click="handleDownload">下载游戏</button>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, onMounted, onUnmounted } from 'vue';
  
  const props = defineProps({
    images: {
      type: Array,
      required: true
    },
    gameTitle: {
      type: String,
      required: true
    },
    gameDescription: {
      type: String,
      required: true
    }
  });
  
  let intervalId = null;
  const carousel = ref(null);
  const currentIndex = ref(0);
  const displayedImages = ref([]);
  
  onMounted(() => {
    displayedImages.value = [...props.images, ...props.images]; // 复制数组以实现循环轮播
    intervalId = setInterval(() => nextImage(), 3000); // 每3秒自动切换
  });
  
  onUnmounted(() => {
    clearInterval(intervalId);
  });
  
  const nextImage = () => {
    currentIndex.value = (currentIndex.value + 1) % displayedImages.value.length;
  };
  
  const prevImage = () => {
    currentIndex.value = (currentIndex.value - 1 + displayedImages.value.length) % displayedImages.value.length;
  };
  
  const handleDownload = () => {
    console.log('游戏下载被点击');
  };
  </script>
  
  <style scoped>
  .game-display {
    display: flex;
    justify-content: space-between;
  }
  
  .carousel {
    position: relative;
    overflow: hidden;
    width: 50%;
  }
  
  .carousel > div {
    position: absolute;
    width: 100%;
    opacity: 0;
    transition: opacity 0.5s ease-in-out;
  }
  
  .carousel > div.active {
    opacity: 1;
  }
  
  .game-description {
    width: 45%;
    padding: 20px;
  }
  
  .download-button button {
    background-color: #007bff;
    color: white;
    border: none;
    padding: 10px 20px;
    cursor: pointer;
    text-align: center;
  }
  </style>